﻿using System;
using System.Web.UI;
using Crimson;
using Crimson.Dao;
using Crimson.Module;
using Ornament.Util;

public partial class Admin_Crimson_Controls_InfoTypeList : UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
        infoTree1.OnSelectedNodeChanged += SelectedNodeChanged;
    }

    private void SelectedNodeChanged(object sender, DataEvent<InfoType> e)
    {
        nameTextBox.Text = e.Data.Name;
        infoTypeTreeSelector2.SelectedInfoType = e.Data.Parent;
        infoTypeTreeSelector2.CanNotSelectedId = e.Data.Id.ToString();
        infoTypeIdHiddenBox.Value = e.Data.Id.ToString();
        if (!panel1.Enabled)
            panel1.Enabled = true;
    }

  

    protected void save_Click(object sender, EventArgs e)
    {
        bool newInfoType = infoTypeIdHiddenBox.Value.Length == 0;

        IInfoTypeDao infoDao = InfoManager.DaoFactory.InfoTypeDao;
        InfoType infoType = newInfoType ? new InfoType() : infoDao.Get(new Guid(infoTypeIdHiddenBox.Value));
        infoType.Name = nameTextBox.Text;
        infoDao.Save(infoType);
        if (infoTypeTreeSelector2.SelectedInfoTypeId != Guid.Empty)
        {
            InfoType parent = infoDao.Get(infoTypeTreeSelector2.SelectedInfoTypeId);
            parent.Add(infoType);
        }

        infoTree1.DataBindTree();
        infoTypeTreeSelector2.DataBindTree();
        ClearEditPanel();
    }

    protected void addNewType(object sender, EventArgs e)
    {
        if (!panel1.Enabled)
            panel1.Enabled = true;
        ClearEditPanel();
    }

    private void ClearEditPanel()
    {
        infoTypeIdHiddenBox.Value = "";
        nameTextBox.Text = "";
        infoTypeTreeSelector2.SelectedInfoType = null;
    }
}